#include <signal.h>
#include "../ourhdr.h"

/****
 * 信号屏蔽 
 * int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oldset);
 * 参数how
 * SIG_BLOCK    
 * 将newset所指向的信号集中所包含的信号加到当前的信号掩码中， 
 * SIG_UNBLOCK
 * 将newset所指向的信号集中所包含的信号移除
 * SIG_SETMASK
 * 设置当前信号掩码
 * 返回值 成功0，-1失败
 * 
 **/

static void sig_quit(int);

int main(void)
{
    sigset_t newmask, oldmask, pendmask;
    if(signal(SIGQUIT, sig_quit) == SIG_ERR)
    {
        err_sys("can't catch SIGQUIT");
    }
    sigemptyset(&newmask);
    sigaddset(&newmask, SIGQUIT);
    if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) <0 )
    {
        err_sys("SIG_BLOCK error");
    }
    sleep(5);
    if(sigpending(&pendmask)<0)
    {
        err_sys("sigpending error");
    }
    if(sigismember(&pendmask, SIGQUIT))
    {
        printf("\nSIGQUIT pending\n");
    }
    if(sigprocmask(SIG_SETMASK, & oldmask, NULL) < 0)
    {
        err_sys("SIG_SETMASK error");
    }
    printf("SIGQUIT unblocked\n");
    sleep(5);
    exit(0);
}

static void sig_quit(int signo)
{
    printf("caught SIGQUIT\n");
    if(signal(SIGQUIT,SIG_DFL) == SIG_ERR)
    {
        err_sys("can't reset SIGQUIT");
    }
    return;
}